package code.rocky.chapter04;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Suggest57 {
    public static void main(String[] args) {
        // demo01();
        demo02();
    }

    private static void demo01() {
        // 接收键盘输入
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            int wordsCount = str.split(" ").length;
            System.out.println(str + " 单词数: " + wordsCount);
        }
    }

    private static void demo02() {
        // 接收键盘输入
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            // 正则表达式对象
            Pattern pattern = Pattern.compile("\\b\\w+\\b");
            // 生成匹配器
            Matcher matcher = pattern.matcher(str);
            // 记录单词数量
            int wordsCount = 0;
            // 遍历查找匹配
            while (matcher.find()) {
                wordsCount++;
            }
            System.out.println(str + " 单词数: " + wordsCount);
        }
    }
}
